#include <iostream>
#include <cstdlib>
#include <ctime>
#include<cstring>
#define random(a,b) (rand()%(b-a+1))+a
using namespace std;
//转化函数
string _switch_(int a){
	if(a==0) return "[0]";
	if(a==1) return "[1]";
	if(a==2) return "[2]";
	if(a==3) return "[3]";
	if(a==4) return "[4]";
	if(a==5) return "[5]";
	if(a==6) return "[6]";
	if(a==7) return "[7]";
	if(a==8) return "[8]";
	else{
		return "0";
	}
}
//主函数
int main(){
	srand((int)time(0));
	int a[12][12];
	//初始化扫雷地图                    
	for(int i=0;i<=12;i++){
		for(int k=0;k<=12;k++){
			a[i][k]=0;
		} 
	}
	//随机生成雷(i为个数) 
	for(int i=0;i<=20;i++){
		int b=random(0,11),c=random(0,11);
		a[b][c]=1;
	}
	//统计雷个数
	int counter=0;
	for(int i=0;i<=11;i++){
		for(int k=0;k<=11;k++){
			if(a[i][k]==1){
				counter++;
			}
		}
	}
	counter2=counter;
	
	//初始化玩家地图
	string b[12][12];
	for(int i=0;i<=11;i++){
		for(int k=0;k<=11;k++){
			b[i][k]="[ ]";
		} 
	}
	//初始化雷图
	int mine_temp[13][13];
	for(int i=0;i<=12;i++){
		for(int k=0;k<=12;k++){
			mine_temp[i][k]=0;
		}
	}
	for(int i=0;i<=11;i++){
		for(int k=0;k<=11;k++){
			if(a[i][k]==1){
				if(i<=11 && i>=0 && k>=0 && k<=11){
					mine_temp[i][k+1]+=1;
					mine_temp[i][k-1]+=1;
					mine_temp[i+1][k]+=1;
					mine_temp[i-1][k]+=1;
					mine_temp[i+1][k+1]+=1;
					mine_temp[i-1][k-1]+=1;
					mine_temp[i+1][k-1]+=1;
					mine_temp[i-1][k+1]+=1;
				}
				else break;			
			}
		}
	}
	for(int k=0;k<=11;k++){
		for(int i=0;i<=11;i++){
			if(a[i][k]>1){
				a[i][k]=0;
		}
		}
	}
	//雷图标记雷
	for(int i=0;i<=11;i++){
		for(int k=0;k<=11;k++){
			if(a[k][i]==1){
				mine_temp[k][i]=9;
			}
		}
	}
	//测试debug
	cout<<"后台扫雷地图:"<<endl; 
	for(int k=0;k<=11;k++){
		for(int i=0;i<=11;i++){
		cout<<a[k][i]<<" ";
	}
	cout<<" ";
	cout<<k;
	cout<<endl;
	}
	cout<<endl;
	for(int i=0;i<=11;i++){
		cout<<i<<" ";
	}
	
	
	cout<<endl;
	cout<<"后台雷图统计:"<<endl; 
	for(int k=0;k<=11;k++){
		for(int i=0;i<=11;i++){
		cout<<mine_temp[k][i]<<" ";
	}
	cout<<" ";
	cout<<k;
	cout<<endl;
	}
	cout<<endl;
	for(int i=0;i<=11;i++){
		cout<<i<<" ";
	}
	

	
	
	cout<<endl;
	cout<<"玩家视图:"<<endl;
	for(int k=0;k<=11;k++){
		for(int i=0;i<=11;i++){
		cout<<b[k][i]<<" ";
	}
	cout<<" ";
	cout<<k;
	cout<<endl;
	cout<<endl;
	}
	for(int i=0;i<=11;i++){
		cout<<i<<"   ";
	}
	//坐牢判断

	cout<<endl;
	int row,column,counter2;
	string Mode;
	int lose=0,win=0;
	while(lose==0 && win==0){
	cout<<endl;
	cout<<"请输入你想要扫的地方，格式为: [行] [列] [模式](F为标记雷;T为翻开此空位)";
	cout<<endl;
	cin>>row>>column>>Mode;
	if(Mode=="Win"){
		cout<<"作弊码已应用"<<endl;
		win=1;
	}
	//胜利判断
	if(counter2==0){
		win=1;
	}
	//首先判断是否是雷 
	if(a[row][column]==1 && Mode=="T"){
		cout<<"小子，你输了哈哈哈哈哈";
		lose=1;
	}
	else{
		lose=0;
	}
	
	if(win==1){
		cout<<"恭喜获胜!!"<<endl;
	}
	//标记雷 
	if(Mode=="F"){
		b[row][column]="[F]";
		if(a[row][column]=1){
			counter2--;
		} 
	}
	//判断不是雷翻开 
	if(Mode=="T" && a[row][column]!=1){
		b[row][column]=_switch_(mine_temp[row][column]);
	}
	//翻开为0的周围的雷
	int stop=0;
	if(Mode=="T" && a[row][column]==0 && mine_temp[row][column]==0){
		while(stop==0){
			//搜索并翻开
			for(int i=0;i<=11;i++){    //向右搜索翻开
				if(mine_temp[row+i][column]==0 && row+i<=11){
					b[row+i][column]=_switch_(mine_temp[row+i][column]);
					//b[row+i-1][column]=_switch_(mine_temp[row+i-1][column]);
					//b[row+i+1][column]=_switch_(mine_temp[row+i+1][column]);
				} 
				if(mine_temp[row+i][column]!=0) break;				
			}
			for(int i=0;i<=11;i++){   //向左搜索翻开
				if(mine_temp[row-i][column]==0 && row-i>=0){
					b[row-i][column]=_switch_(mine_temp[row-i][column]);
					//b[row-i+1][column]=_switch_(mine_temp[row-i+1][column]);
					//b[row-i-1][column]=_switch_(mine_temp[row-i-1][column]);					
				} 
				if(mine_temp[row-i][column]!=0) break;					
			}
			for(int k=0;k<=11;k++){ //向下搜索翻开
				if(mine_temp[row][column+k]==0 && column+k<=11){
					b[row][column+k]=_switch_(mine_temp[row][column+k]);
					//b[row][column+k+1]=_switch_(mine_temp[row][column+k+1]);
					//b[row][column+k-1]=_switch_(mine_temp[row][column+k-1]);
				} 
				if(mine_temp[row][column+k]!=0) break;
			}
			for(int k=0;k<=11;k++){  //向上搜索翻开
				if(mine_temp[row][column-k]==0 && column-k>=0){
					b[row][column-k]=_switch_(mine_temp[row][column-k]);
					//b[row][column-k+1]=_switch_(mine_temp[row][column+1]);
					//b[row][column+k-1]=_switch_(mine_temp[row][column+k-1]);
				}
				if(mine_temp[row][column-k]!=0) break;
			}
			/*for(int k=0;k<=11;k++){  //斜角搜索翻开
				if(mine_temp[row+k][column+k]==0 && column+k<=11 && row+k<=11) b[row+k][column+k]=_switch_(mine_temp[row+k][column+k]);
				if(mine_temp[row+k][column+k]!=0) break;
			}
			for(int k=0;k<=11;k++){ 
				if(mine_temp[row-k][column+k]==0 && column-k>=0 && row+k<=11) b[row-k][column+k]=_switch_(mine_temp[row-k][column+k]);
				if(mine_temp[row-k][column+k]!=0) break;
			}
			for(int k=0;k<=11;k++){ 
				if(mine_temp[row-k][column-k]==0 && column-k>=0 && row-k>=0) b[row-k][column-k]=_switch_(mine_temp[row-k][column-k]);
				if(mine_temp[row-k][column-k]!=0) break;
			}
			for(int k=0;k<=11;k++){ 
				if(mine_temp[row+k][column-k]==0 && column+k<=11 && row-k>=0) b[row+k][column-k]=_switch_(mine_temp[row+k][column-k]);
				if(mine_temp[row+k][column-k]!=0) break;
			}*/
			stop=1;
		}
	}
	//判断翻开
	if(Mode=="T" && mine_temp[row][column]==0){
		for(int o=0;o<=12;o++){
			for(int i=1;i<11;i++){
				for(int k=1;k<11;k++){
					if(b[i][k]=="[0]"){
						b[i+1][k]=_switch_(mine_temp[i+1][k]);
						b[i-1][k]=_switch_(mine_temp[i-1][k]);
						b[i][k+1]=_switch_(mine_temp[i][k+1]);
						b[i][k-1]=_switch_(mine_temp[i][k-1]);
						b[i+1][k+1]=_switch_(mine_temp[i+1][k+1]);
						b[i+1][k-1]=_switch_(mine_temp[i+1][k-1]);
						b[i-1][k-1]=_switch_(mine_temp[i-1][k-1]);
						b[i-1][k+1]=_switch_(mine_temp[i-1][k+1]);
					}
				}
			}
		}
	}
	//显示玩家视图 
	cout<<endl;
	cout<<"玩家视图:"<<endl;
	for(int k=0;k<=11;k++){
		for(int i=0;i<=11;i++){
		cout<<b[k][i]<<" ";
	}
	cout<<" ";
	cout<<k;
	cout<<endl;
	cout<<endl;
	}
	for(int i=0;i<=11;i++){
		cout<<i<<"   ";
		}
	cout<<endl;
	cout<<"剩余雷个数: "<<counter;
		
	}

	
} 